﻿using QunProject.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnumHelper = QunProject.Model.EnumHelper;

namespace QunProject.Manage.Controllers
{
    public class ArticleController : BaseController
    {
        // 首页
        public ActionResult Index()
        {
            Cache.Clear("Article_AjaxData_member");
            Cache.Clear("Article_AjaxData_special");
            Cache.Clear("Article_AjaxData_classify");
            return View();
        }
        // 预览页
        public ActionResult Preview(int? id)
        {
            var data = EF.article.SingleOrDefault(x => x.ID == id);
            ViewBag.Content = data.Content;
            return View();
        }
        // 编辑幻灯片
        public ActionResult EditBanner(int? id)
        {
            ViewBag.ID = id;
            return View();
        }
        // 编辑友情链接
        public ActionResult EditFriend(int? id)
        {
            ViewBag.ID = id;
            return View();
        }
        // 编辑公告
        public ActionResult EditProclaim(int? id)
        {
            ViewBag.ID = id;
            return View();
        }


        // 获取数据
        public JsonResult AjaxData(int? qSpecial, int? qType, int? qArticleId, int page = 1, int limit = 20, string qMember = "", string qTitle = "")
        {
            List<Model.member> member = null;
            var cache_member = Cache.Get("Article_AjaxData_member");
            if (cache_member == null)
            {
                member = EF.member.ToList();
                Cache.Set("Article_AjaxData_member", member);
            }
            else
            {
                member = (List<Model.member>)cache_member;
            }
            List<Model.special> special = null;
            var cache_special = Cache.Get("Article_AjaxData_special");
            if (cache_special == null)
            {
                special = EF.special.ToList();
                Cache.Set("Article_AjaxData_special", special);
            }
            else
            {
                special = (List<Model.special>)cache_special;
            }
            List<Model.classify> classify = null;
            var cache_classify = Cache.Get("Article_AjaxData_classify");
            if (cache_classify == null)
            {
                classify = EF.classify.ToList();
                Cache.Set("Article_AjaxData_classify", classify);
            }
            else
            {
                classify = (List<Model.classify>)cache_classify;
            }

            var list = from a in EF.article.ToList()
                       join m in member on a.MemberID equals m.ID into Tam
                       from tam in Tam.DefaultIfEmpty(new Model.member { Name = "-", Phone = "-", Number = "-" })
                       join s in special on a.MemberID equals s.MemberID into Tas
                       from tas in Tas.DefaultIfEmpty(new Model.special { Name = "-" })
                       join c in classify on a.ClassifyID equals c.ID into Tac
                       from tac in Tac.DefaultIfEmpty(new Model.classify { Name = "-" })
                       where (tam.Phone.Contains(qMember) || tam.Name.Contains(qMember) || tam.Number.Contains(qMember))
                        && (qSpecial.HasValue ? a.SpecialID == qSpecial : true)
                        && (qType.HasValue ? a.Types == qType : true)
                        && (qArticleId.HasValue ? a.ID == qArticleId : true)
                        && a.Title.Contains(qTitle)
                       orderby a.CreateTime descending
                       select new
                       {
                           a.ID,
                           a.CreateTime,
                           a.Remark,
                           a.Title,
                           a.MemberID,
                           a.Audit,
                           a.AuditReason,
                           a.Source,
                           a.SourceUrl,
                           a.ClickAmount,
                           a.Password,
                           a.Img,
                           a.Praise,
                           a.Criticism,
                           a.Tops,
                           a.IsReply,
                           a.Types,
                           a.ClassifyID,
                           a.SpecialID,
                           a.Sort,
                           ClassifyName = tac.Name,
                           SpecialName = tas.Name,
                           tam.Phone,
                           UserName = tam.Name,
                           tam.Number
                       };
            var data = list.OrderByDescending(x => x.CreateTime).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
            {
                x.ID,
                CreateTime = x.CreateTime.Value.ToString("yyyy-MM-dd hh:mm:ss"),
                x.Remark,
                x.Title,
                x.MemberID,
                x.Audit,
                x.AuditReason,
                x.Source,
                x.SourceUrl,
                x.ClickAmount,
                IsPassword = x.Password.IsNullOrWhiteSpace() ? false : true,
                x.Password,
                x.Img,
                x.Praise,
                x.Criticism,
                x.Tops,
                x.IsReply,
                TypeName = Enum.GetName(typeof(EnumHelper.ArticleType), x.Types),
                x.Types,
                x.ClassifyID,
                x.SpecialID,
                x.Sort,
                x.ClassifyName,
                x.SpecialName,
                x.Phone,
                x.UserName,
                x.Number
            });
            return Json(Result.SuccessReturnData(list.Count(), data));
        }
        // 置顶
        public JsonResult AjaxTop(int id)
        {
            var model = EF.article.SingleOrDefault(x => x.ID == id);
            if (model == null)
                return Json(Result.Fail("不存在此文章"));

            model = EF.article.Attach(model);
            model.Tops = !model.Tops;

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 审核
        public JsonResult AjaxAudit(int ID, int Audit, string AuditReason = "")
        {
            var model = EF.article.SingleOrDefault(x => x.ID == ID);
            if (model == null)
                return Json(Result.Fail("不存在此文章"));

            model = EF.article.Attach(model);
            model.Audit = Audit;
            model.AuditReason = AuditReason;

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 重置密码
        public JsonResult AjaxReload(int id)
        {
            var model = EF.article.SingleOrDefault(x => x.ID == id);
            if (model == null)
                return Json(Result.Fail("不存在此文章"));

            model = EF.article.Attach(model);
            model.Password = UserDefaultPassword;

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail("密码未改变"));
        }
        // 查询实体
        public JsonResult AjaxEntity(int id)
        {
            var data = EF.article.SingleOrDefault(x => x.ID == id);
            if (data == null)
                return Json(Result.Fail("不存在此管理员"));

            var model = new
            {
                data.ID,
                data.CreateTime,
                data.Remark,
                data.Title,
                data.Content,
                data.MemberID,
                Audit = Enum.GetName(typeof(EnumHelper.AuditType), data.Audit),
                data.AuditReason,
                data.Source,
                data.SourceUrl,
                data.ClickAmount,
                data.Password,
                data.Img,
                data.Praise,
                data.Criticism,
                data.Tops,
                data.IsReply,
                TypeName = Enum.GetName(typeof(EnumHelper.ArticleType), data.Types),
                data.ClassifyID,
                data.SpecialID,
                data.Sort

            };
            return Json(Result.Success(model));
        }
        // 提交编辑幻灯片
        public ActionResult AjaxEditBanner(Model.article model)
        {
            Model.article _model = new Model.article();
            if (!Common.Verify.VerifyLength(model.Title, 4, 16))
                return Json(Result.Fail("标题长度为4-16"));

            if (model.ID > 0)
            {
                _model = EF.article.SingleOrDefault(x => x.ID == model.ID);
                _model = EF.article.Attach(_model);
            }
            else
            {
                _model.Audit = (int)EnumHelper.AuditType.审核通过;
                _model.ClickAmount = 0;
                _model.CreateTime = DateTime.Now;
                _model.Criticism = 0;
                _model.Praise = 0;
                _model.Source = string.Empty;
                _model.Types = (int)EnumHelper.ArticleType.幻灯片;
                _model.Tops = false;
                _model.AuditReason = string.Empty;
                _model.Content = string.Empty;
                _model.Password = string.Empty;
                _model.IsValid = true;
                _model.IsReply = false;
            }
            _model.SourceUrl = model.SourceUrl;
            _model.Remark = model.Remark;
            _model.Title = model.Title;
            _model.Img = model.Img;
            _model.Sort = model.Sort;

            bool isSucc = false;
            if (model.ID > 0)
            {
                isSucc = EF.SaveChanges() > 0;
            }
            else
            {
                EF.article.Add(_model);
                isSucc = EF.SaveChanges() > 0;
            }
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 提交编辑友情链接
        public ActionResult AjaxEditFriend(Model.article model)
        {
            Model.article _model = new Model.article();
            if (!Common.Verify.VerifyLength(model.Source, 2, 16))
                return Json(Result.Fail("标题长度为2-16"));

            if (model.ID > 0)
            {
                _model = EF.article.SingleOrDefault(x => x.ID == model.ID);
                _model = EF.article.Attach(_model);
            }
            else
            {
                _model.Audit = (int)EnumHelper.AuditType.审核通过;
                _model.ClickAmount = 0;
                _model.CreateTime = DateTime.Now;
                _model.Criticism = 0;
                _model.Praise = 0;
                _model.Types = (int)EnumHelper.ArticleType.友情链接;
                _model.Tops = false;
                _model.AuditReason = string.Empty;
                _model.Content = string.Empty;
                _model.Password = string.Empty;
                _model.Img = string.Empty;
                _model.IsReply = false;
                _model.IsValid = true;
                _model.Title = string.Empty;
            }
            _model.Remark = model.Remark;
            _model.Sort = model.Sort;
            _model.Source = model.Source;
            _model.SourceUrl = model.SourceUrl;

            bool isSucc = false;
            if (model.ID > 0)
            {
                isSucc = EF.SaveChanges() > 0;
            }
            else
            {
                EF.article.Add(_model);
                isSucc = EF.SaveChanges() > 0;
            }
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 提交编辑公告
        [ValidateInput(false)]
        public ActionResult AjaxEditProclaim(Model.article model)
        {
            Model.article _model = new Model.article();

            if (model.ID > 0)
            {
                _model = EF.article.SingleOrDefault(x => x.ID == model.ID);
                _model = EF.article.Attach(_model);
            }
            else
            {
                _model.Audit = (int)EnumHelper.AuditType.审核通过;
                _model.ClickAmount = 0;
                _model.CreateTime = DateTime.Now;
                _model.Criticism = 0;
                _model.Praise = 0;
                _model.Types = (int)EnumHelper.ArticleType.网站公告;
                _model.Tops = false;
                _model.AuditReason = string.Empty;
                _model.Password = string.Empty;
                _model.Img = string.Empty;
                _model.Sort = 0;
                _model.Source = string.Empty;
                _model.SourceUrl = string.Empty;
                _model.IsValid = true;
            }
            _model.Title = model.Title;
            _model.IsReply = model.IsReply;
            _model.Content = model.Content;
            _model.Remark = model.Remark;

            bool isSucc = false;
            if (model.ID > 0)
            {
                isSucc = EF.SaveChanges() > 0;
            }
            else
            {
                EF.article.Add(_model);
                isSucc = EF.SaveChanges() > 0;
            }
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
    }
}